package com.ayu.product.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 徐林玉//作者及
 * @Date: 2021/9/27//完成日期
 * @Description: //
 * @Version: v0.0.1 // 版本信息
 * @Function List: // 主要函数及其功能
 * @Others: // 其它内容的说明
 * @History: // 历史修改记录
 **/

@Slf4j
public class OutPutJson
{

	public static void success(HttpServletResponse response, Integer status, String msg)
	{

		try
		{
			response.setContentType("application/json;charset=utf-8");
			response.setStatus(status);
			PrintWriter writer = null;
			writer = response.getWriter();
			Map<String, Object> resultMap = new HashMap<>();
			resultMap.put("code", status);
			resultMap.put("msg", msg);
			writer.write(new ObjectMapper().writeValueAsString(resultMap));
			writer.flush();
			writer.close();

		} catch (IOException e)
		{
			log.info(e.getMessage());
		}

	}

	/**
	 * 读取inputStream的值
	 * @param inStream
	 * @return
	 * @throws Exception
	 */
	public static byte[] readInputStream(InputStream inStream) throws Exception {
		ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		boolean var3 = false;

		int len;
		while((len = inStream.read(buffer)) != -1) {
			outSteam.write(buffer, 0, len);
		}

		outSteam.close();
		inStream.close();
		return outSteam.toByteArray();
	}

}
